package co.lostreseditores.colegio.presentation.backingBeans;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.CloseEvent;
import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.ResizeEvent;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.ToggleEvent;
import org.primefaces.component.layout.LayoutUnit;
import org.primefaces.component.selectonemenu.SelectOneMenu;

import co.lostreseditores.colegio.exceptions.ZMessManager;
import co.lostreseditores.colegio.modelo.Colegio;
import co.lostreseditores.colegio.modelo.Lugar;
import co.lostreseditores.colegio.modelo.dto.ColegioDTO;
import co.lostreseditores.colegio.presentation.businessDelegate.BusinessDelegatorView;
import co.lostreseditores.colegio.utilities.FacesUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 * 
 * @author Zathura Code Generator http://code.google.com/p/zathura
 * 
 */
public class ColegioView {
	private InputText txtColecoddanen;
	private InputText txtColedescripv;
	private InputText txtLugaidn;
	private InputText txtColeidn;
	private CommandButton btnSave;
	private CommandButton btnModify;
	private CommandButton btnDelete;
	private CommandButton btnClear;
	private List<ColegioDTO> data;
	private ColegioDTO selectedColegio;

	private SelectOneMenu dpto;
	private SelectOneMenu ciudad;
	private List<Lugar> listDpto = new ArrayList<Lugar>();
	private List<Lugar> listCiu = new ArrayList<Lugar>();
	private List<SelectItem> listCiudad;
	private String dptoSelect;
	private Integer ciuSelect;

	public ColegioView() {
		super();

	}

	public void rowEventListener(RowEditEvent e) {
		try {
			ColegioDTO colegioDTO = (ColegioDTO) e.getObject();

			if (txtColecoddanen == null) {
				txtColecoddanen = new InputText();
			}

			txtColecoddanen.setValue(colegioDTO.getColecoddanen());

			if (txtColedescripv == null) {
				txtColedescripv = new InputText();
			}

			txtColedescripv.setValue(colegioDTO.getColedescripv());

			if (txtLugaidn == null) {
				txtLugaidn = new InputText();
			}

			txtLugaidn.setValue(colegioDTO.getLugaidn());

			if (txtColeidn == null) {
				txtColeidn = new InputText();
			}

			txtColeidn.setValue(colegioDTO.getColeidn());
			// ////////////////////////////////////////////acomodar cuando este
			// listo el update!!!!!!
			// action_modify();
		} catch (Exception ex) {
		}
	}

	public String action_clear() {
		if (txtColecoddanen != null) {
			txtColecoddanen.setValue(null);
			txtColecoddanen.setDisabled(true);
		}

		if (txtColedescripv != null) {
			txtColedescripv.setValue(null);
			txtColedescripv.setDisabled(true);
		}

		if (txtLugaidn != null) {
			txtLugaidn.setValue(null);
			txtLugaidn.setDisabled(true);
		}

		if (txtColeidn != null) {
			txtColeidn.setValue(null);
			txtColeidn.setDisabled(false);
		}

		if (btnSave != null) {
			btnSave.setDisabled(true);
		}

		if (btnDelete != null) {
			btnDelete.setDisabled(true);
		}

		if (btnModify != null) {
			btnModify.setDisabled(true);
		}

		if (btnClear != null) {
			btnClear.setDisabled(false);
		}

		return "";
	}

	public void listener_txtDane() {
		Colegio entity = null;
		Lugar lug = null;

		try {
			Long coleDane = new Long(txtColecoddanen.getValue().toString());
			entity = BusinessDelegatorView.getColegioDane(coleDane);
		

		if (entity == null) {
			txtColecoddanen.setDisabled(false);
			txtColedescripv.setDisabled(false);
			btnSave.setDisabled(false);
			btnDelete.setDisabled(true);
			btnModify.setDisabled(true);
			btnClear.setDisabled(false);
		} else {			
				lug = BusinessDelegatorView.getLugarDane(entity.getLugaidn());
				listCiudad.add(new SelectItem(lug.getLugacoddanen(),lug.getLugadescripv()));
				txtColecoddanen.setDisabled(false);
				txtColedescripv.setValue(entity.getColedescripv());
				txtColedescripv.setDisabled(false);
				btnSave.setDisabled(true);
				btnDelete.setDisabled(false);
				btnModify.setDisabled(false);
				btnClear.setDisabled(false);
		} 
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void listener_CmbDpto() {
		//System.out.println(dptoSelect);
		try {
			if (!dptoSelect.equals("0")) {
				listCiu = BusinessDelegatorView.getCiudades(Integer
						.parseInt(dptoSelect));
				if (listCiu != null && listCiu.size() > 0) {
					listCiudad = new ArrayList<SelectItem>();
					for (Lugar lugar : listCiu) {
						listCiudad.add(new SelectItem(lugar.getLugacoddanen(), lugar
								.getLugadescripv()));
					}
				}
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public List<SelectItem> getListCiudad() {
		return listCiudad;
	}

	public void setListCiudad(List<SelectItem> listCiudad) {
		this.listCiudad = listCiudad;
	}

	public String action_save() {
		try {
			BusinessDelegatorView.saveColegio(
					FacesUtils.checkLong(txtColecoddanen),
					FacesUtils.checkString(txtColedescripv), null,
					ciuSelect.longValue());
			// FacesUtils.checkLong(txtLugaidn));
			// 5001L);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
			action_clear();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete() {
		try {
			BusinessDelegatorView.deleteColegio(FacesUtils
					.checkInteger(txtColeidn));
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	/*
	 * public String action_modify() { try {
	 * BusinessDelegatorView.updateColegio(
	 * FacesUtils.checkLong(txtColecoddanen),
	 * FacesUtils.checkString(txtColedescripv),
	 * FacesUtils.checkInteger(txtColeidn), FacesUtils.checkLong(txtLugaidn));
	 * FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
	 * action_clear(); } catch (Exception e) { data = null;
	 * FacesUtils.addErrorMessage(e.getMessage()); }
	 * 
	 * return ""; }
	 */

	public String actionDeleteDataTableEditable() {
		try {
			if (txtColeidn == null) {
				txtColeidn = new InputText();
			}

			txtColeidn.setValue(selectedColegio.getColeidn());

			BusinessDelegatorView.deleteColegio(FacesUtils
					.checkInteger(txtColeidn));
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
			data.remove(selectedColegio);
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	/*public String action_modifyWitDTO(Long colecoddanen, String coledescripv,
			String coledireccionv, String coleemailv, String colefaxv,
			Integer coleidn, String colelogov, String colesectorv,
			String coletelefonofv, String colezonav, Long lugaidn,
			Integer comuidn_Comunidad) throws Exception {
		try {
			BusinessDelegatorView.updateColegio(colecoddanen, coledescripv,
					coledireccionv, coleemailv, colefaxv, coleidn, colelogov,
					colesectorv, coletelefonofv, colezonav, lugaidn,
					comuidn_Comunidad);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			// renderManager.getOnDemandRenderer("ColegioView").requestRender();
			FacesUtils.addErrorMessage(e.getMessage());
			throw e;
		}

		return "";
	}*/

	public InputText getTxtColecoddanen() {
		return txtColecoddanen;
	}

	public void setTxtColecoddanen(InputText txtColecoddanen) {
		this.txtColecoddanen = txtColecoddanen;
	}

	public InputText getTxtColedescripv() {
		return txtColedescripv;
	}

	public void setTxtColedescripv(InputText txtColedescripv) {
		this.txtColedescripv = txtColedescripv;
	}

	public InputText getTxtLugaidn() {
		return txtLugaidn;
	}

	public void setTxtLugaidn(InputText txtLugaidn) {
		this.txtLugaidn = txtLugaidn;
	}

	public InputText getTxtColeidn() {
		return txtColeidn;
	}

	public void setTxtColeidn(InputText txtColeidn) {
		this.txtColeidn = txtColeidn;
	}

	public List<ColegioDTO> getData() {
		try {
			if (data == null) {
				data = BusinessDelegatorView.getDataColegio();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return data;
	}

	public void setData(List<ColegioDTO> colegioDTO) {
		this.data = colegioDTO;
	}

	public ColegioDTO getSelectedColegio() {
		return selectedColegio;
	}

	public void setSelectedColegio(ColegioDTO colegio) {
		this.selectedColegio = colegio;
	}

	public CommandButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(CommandButton btnSave) {
		this.btnSave = btnSave;
	}

	public CommandButton getBtnModify() {
		return btnModify;
	}

	public void setBtnModify(CommandButton btnModify) {
		this.btnModify = btnModify;
	}

	public CommandButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(CommandButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public CommandButton getBtnClear() {
		return btnClear;
	}

	public void setBtnClear(CommandButton btnClear) {
		this.btnClear = btnClear;
	}

	public TimeZone getTimeZone() {
		return java.util.TimeZone.getDefault();
	}

	/*
	 * public Lugar getCmbDpto() { return cmbDpto; }
	 * 
	 * public void setCmbDpto(Lugar cmbDpto) { this.cmbDpto = cmbDpto; }
	 */

	public List<Lugar> getListDpto() {
		listCiu.clear();
		listDpto.clear();
		try {
			listDpto = BusinessDelegatorView.getDptos();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listDpto;
	}

	public void setListDpto(List<Lugar> listDpto) {
		this.listDpto = listDpto;
	}

	/*
	 * public Lugar getCmbCui() { return cmbCui; }
	 * 
	 * public void setCmbCui(Lugar cnbCui) { this.cmbCui = cnbCui; }
	 */

	public List<Lugar> getListCiu() {
		return listCiu;
	}

	public void setListCiu(List<Lugar> listCiu) {
		this.listCiu = listCiu;
	}

	public String getDptoSelect() {
		return dptoSelect;
	}

	public void setDptoSelect(String dptoSelect) {
		this.dptoSelect = dptoSelect;
	}

	public Integer getCiuSelect() {
		return ciuSelect;
	}

	public void setCiuSelect(Integer ciuSelect) {
		this.ciuSelect = ciuSelect;
	}

	public SelectOneMenu getDpto() {
		return dpto;
	}

	public void setDpto(SelectOneMenu dpto) {
		this.dpto = dpto;
	}

	public SelectOneMenu getCiudad() {
		return ciudad;
	}

	public void setCiudad(SelectOneMenu ciudad) {
		this.ciudad = ciudad;
	}

}